import {
  setToken,
  removeToken,
  setCurrentEnterprise,
  removeCurrentEnterprise,
  removeUserInfo,
  removeCurrentSite,
  setUserInfo,
  setCurrentSite,
  removeSiteList,
  removeEnterpriseList, setSiteList, setEnterpriseList
} from '@/utils/auth'
import { resetRouter } from '@/router'
import { getAllApprovedEnterprises, getAllApprovedSiteByEnterpriseId } from '@/api/enter'
import { getUserInfo } from '@/api/system'
import router from '@/router'

const getDefaultState = () => {
  return {
    userInfo: {},
    currentEnterprise: {},
    currentSite: {}
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_USERINFO: (state, userInfo) => {
    state.userInfo = userInfo
  },
  SET_CURRENT_ENTERPRISE: (state, currentEnterprise) => {
    state.currentEnterprise = currentEnterprise
  },
  SET_CURRENT_SITE: (state, currentSite) => {
    state.currentSite = currentSite
  }
}

const actions = {
  // 登录
  login({ commit }, params) {
    return new Promise((resolve, reject) => {
      const { data, loginApi } = params
      loginApi(data).then(res => {
        const { token } = res
        setToken(token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取登录账号企业列表
  getEnterprisesList({ commit }) {
    return new Promise((resolve, reject) => {
      getAllApprovedEnterprises().then(res => {
        const enterpriseList = res || []
        if (enterpriseList.length) {
          setEnterpriseList(enterpriseList)
          setCurrentEnterprise(enterpriseList[0])
          commit('SET_CURRENT_ENTERPRISE', enterpriseList[0])
        }
        resolve(enterpriseList)
      }).catch(error => {
        reject(error)
      })
    })
  },

  setCurrentEnterprise({ commit }, data) {
    commit('SET_CURRENT_ENTERPRISE', data)
  },

  // 获取当前企业下的站点列表
  getSiteList({ commit }, params) {
    return new Promise((resolve, reject) => {
      getAllApprovedSiteByEnterpriseId(params).then(res => {
        const siteList = res || []
        if (siteList.length) {
          setSiteList(siteList)
          setCurrentSite(siteList[0])
          commit('SET_CURRENT_SITE', siteList[0])
          resolve(siteList)
        } else {
          router.push({
            path: '/enterProcess/enter'
          })
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

  setCurrentSite({ commit }, data) {
    commit('SET_CURRENT_SITE', data)
  },

  // 获取登录账号信息
  getUserInfo({ commit }) {
    return new Promise((resolve, reject) => {
      getUserInfo().then(res => {
        const userInfo = res || {}
        setUserInfo(userInfo)
        commit('SET_USERINFO', userInfo)
        resolve(userInfo)
      }).catch(error => {
        reject(error)
      })
    })
  },

  setUserInfo({ commit }, data) {
    commit('SET_USERINFO', data)
  },

  // user logout
  logout({ commit, state }) {
    removeToken() // must remove  token  first
    removeUserInfo()
    removeCurrentEnterprise()
    removeCurrentSite()
    removeSiteList()
    removeEnterpriseList()
    resetRouter()
    commit('RESET_STATE')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

